//
//  OMHomeViewController.m
//  Weibbo
//
//  Created by ifuninfo on 13-6-9.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "OMHomeViewController.h"
#import "OMSettingViewController.h"
#import "OMPreviewController.h"
#import "HomeBottomView.h"
#import "HelpView.h"
#import "Animations.h"

@interface OMHomeViewController ()<HGPageScrollViewDelegate,HomeBottomViewDelegate,HelpViewDelegate>
{
    HelpView  *helpInfoView;
    BOOL       isRemovHelpInfoView;
}



@end

@implementation OMHomeViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isRemovHelpInfoView = YES;
    UIImageView  *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-20-45)];
    [backImageView setImage:[UIImage imageNamed:@"home_backImage"]];
    [self.view addSubview:backImageView];
    [backImageView release];
    
    _pageScrollView = [[[[NSBundle mainBundle] loadNibNamed:@"HGPageScrollView" owner:self options:nil] objectAtIndex:0] retain];
    [_pageScrollView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_pageScrollView setDataSource:self];
    [_pageScrollView setDelegate:self];
    [self.view addSubview:_pageScrollView];


    
    homeBottomView = [[HomeBottomView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 20 - 44, SCREEN_WIDTH, 49)];
    homeBottomView.delegate = self;
    homeBottomView.tag = 10000;
    [_pageScrollView addSubview:homeBottomView];

    [_pageScrollView selectPageAtIndex:0 animated:NO];

}
- (void)viewDidUnload
{
    UIRelease(_pageScrollView);
    UIRelease(homeBottomView);
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)dealloc
{
    UIRelease(_pageScrollView);
    UIRelease(homeBottomView);
    [super dealloc];
}

-(void)sendScrollView:(HGPageScrollView *)scrlloview front:(BOOL)flag
{
    if (flag) {
        UIView *bottomView = [scrlloview viewWithTag:10000];
        [scrlloview bringSubviewToFront:bottomView];
    }
    else
    {
        UIView *bottomView = [scrlloview viewWithTag:10000];
        [scrlloview sendSubviewToBack:bottomView];
    }
}
#pragma mark - HomeBottomViewDelegate method
-(void)HomeBottomViewDelegateSetting
{
    OMSettingViewController *settingViewcontroller=[[OMSettingViewController alloc]init];
    [self.navigationController pushViewController:settingViewcontroller animated:YES];
    [settingViewcontroller release];

}

-(void)HomeBottomViewDelegateAddLongWeibo
{
    DraftEntity  *entity = [[DraftEntity alloc] init];
    [WBBDRAFT insertDraft:entity];
    [_pageScrollView reloadData];
    [_pageScrollView scrollToPageAtIndex:0 animated:YES];
}

-(void)HomeBottomViewDelegateHelpHiden
{
    isRemovHelpInfoView = !isRemovHelpInfoView;
    if (!isRemovHelpInfoView)
    {
        if (!helpInfoView) {
            helpInfoView = [[HelpView alloc]initWithFrame:CGRectMake(30, SCREEN_HEIGHT, SCREEN_WIDTH-60, SCREEN_WIDTH-40)];
            helpInfoView.delagate = self;
            [self.view addSubview:helpInfoView];
        }
        CGRect frame = helpInfoView.frame;
        frame.origin.y = SCREEN_HEIGHT-frame.size.height-70;
        helpInfoView.frame = frame;
        [Animations shadowOnView:helpInfoView andShadowType:@"Elliptical"];
        [Animations moveUp:helpInfoView andAnimationDuration:0.1 andWait:YES andLength:40.0];
        [Animations moveDown:helpInfoView andAnimationDuration:0.1 andWait:YES andLength:20.0];
        [Animations moveUp:helpInfoView andAnimationDuration:0.1 andWait:YES andLength:5.0];
        
    }
    else
    {
        [Animations shadowOnView:helpInfoView andShadowType:@"Elliptical"];
        [Animations moveUp:helpInfoView andAnimationDuration:0.2 andWait:YES andLength:25.0];
        __block HelpView *helpInfoViewRef = helpInfoView;
        [UIView animateWithDuration:0.25 animations:^{
            CGRect frame = helpInfoView.frame;
            frame.origin.y = SCREEN_HEIGHT;
            helpInfoViewRef.frame = frame;
        }
         completion:^(BOOL finished) {
             [helpInfoView removeAllSubviews];
             [helpInfoView removeFromSuperview];
             UIRelease(helpInfoView);
         }];
    }
}

-(void)HelpViewDelegateRemove
{
    isRemovHelpInfoView = YES;
    [Animations shadowOnView:helpInfoView andShadowType:@"Elliptical"];
    [Animations moveUp:helpInfoView andAnimationDuration:0.2 andWait:YES andLength:25.0];
    __block HelpView *helpInfoViewRef = helpInfoView;
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = helpInfoView.frame;
        frame.origin.y = SCREEN_HEIGHT;
        helpInfoViewRef.frame = frame;
    }
                     completion:^(BOOL finished) {
                         [helpInfoView removeAllSubviews];
                         [helpInfoView removeFromSuperview];
                         UIRelease(helpInfoView);
                     }];
}
#pragma mark - HGPageScrollViewDelegate method

- (HGPageView *)pageScrollView:(HGPageScrollView *)scrollView viewForPageAtIndex:(NSInteger)index
{
     static NSString *identifier = @"pageView";
    HGPageView  *page = [scrollView dequeueReusablePageWithIdentifier:identifier];
    if (page == nil)
    {
       __block OMPreviewController *controller = [[OMPreviewController alloc]init];
        [controller setPreviewStyle:YES];
        page = (HGPageView *)controller.view;
        [page setReuseIdentifier:identifier];
        
        [controller setDismiss:^(void){
            [self sendScrollView:scrollView front:YES];
            [scrollView deselectPageAnimated:YES];
        }];
        [page setParentController:controller];
    }
    OMPreviewController *controller = [page parentController];
    [controller setDraft:[WBBDRAFT draftAtIndex:index]];
    [page setTag:index];
    return page;
}
- (NSInteger)numberOfPagesInScrollView:(HGPageScrollView *)scrollView
{
    return [WBBDRAFT count];
}
- (void)pageScrollView:(HGPageScrollView *)scrollView willSelectPageAtIndex:(NSInteger)index
{
    [self sendScrollView:scrollView front:NO];
   HGPageView *view =  [scrollView pageAtIndex:index];
    [[view parentController] setPreviewStyle:NO];
}

@end
